package com.ruoyi;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

@Component
public class ComTreeNode {
    @Autowired
    private TreeNode treeNode;


    public TreeNode createTree() {
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        root.left.left = new TreeNode(4);
        root.left.right = new TreeNode(5);
        root.right.left = new TreeNode(6);
        root.right.right = new TreeNode(7);
        return root;
    }
    public void printWithLineFeed(TreeNode root) {
        if (root == null) {
            return;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        int nextLevel = 0;
        int toBePrinted = 1;

        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            System.out.print(node.val + " ");

            if (node.left != null) {
                queue.offer(node.left);
                nextLevel++;
            }
            if (node.right != null) {
                queue.offer(node.right);
                nextLevel++;
            }

            toBePrinted--;
            if (toBePrinted == 0) {
                System.out.println();
                toBePrinted = nextLevel;
                nextLevel = 0;
            }
        }
    }
    /*
    * @author:chengyu
    * @date:2024/10/25
    * @fun:层序遍历
    */
    List<Integer> levelOrder(TreeNode root) {
        // 初始化队列，加入根节点
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        // 初始化一个列表，用于保存遍历序列
        List<Integer> list = new ArrayList<>();
        while (!queue.isEmpty()) {
            TreeNode node = queue.poll(); // 队列出队
            list.add(node.val);           // 保存节点值
            if (node.left != null)
                queue.offer(node.left);
            if (node.right != null)
                queue.offer(node.right);
        }
        return list;
    }
    /**
    *@Date：2024/10/25
    *@Author：chengyu
    *@fun：前序遍历
    */
//    @Test
    public void testOrder(){
        TreeNode root = createTree();
        List<Integer> preList = new ArrayList<>();
        List<Integer> inList = new ArrayList<>();
        List<Integer> postList = new ArrayList<>();
        System.out.println("前序遍历:" + preOrder(root, preList));
        System.out.println("中序遍历:" + inOrder(root, inList));
        System.out.println("后序遍历:" + postOrder(root, postList));
    }

    public List<Integer> preOrder(TreeNode root, List<Integer> list){
        if (root == null)
            return null;
        list.add(root.val);
        preOrder(root.left, list);
        preOrder(root.right, list);
        return list;
    }

    /**
    *@Date：2024/10/25
    *@Author：chengyu
    *@fun：中序遍历
    */
    public List<Integer> inOrder(TreeNode root, List<Integer> list){
        if (root == null)
            return null;
        inOrder(root.left, list);
        list.add(root.val);
        inOrder(root.right, list);
        return list;
    }
    /**
    *@Date：2024/10/25
    *@Author：chengyu
    *@fun：后序遍历
    */
    public List<Integer> postOrder(TreeNode root, List<Integer> list){
        if (root == null)
            return null;
        postOrder(root.left, list);
        postOrder(root.right, list);
        list.add(root.val);
        return list;
    }

}
